package com.ruoyi.project.web.examPaper.controller;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.web.quesInfo.domain.QuesInfo;
import com.ruoyi.project.web.quesInfo.service.IQuesInfoService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.web.examPaper.domain.ExamPaper;
import com.ruoyi.project.web.examPaper.service.IExamPaperService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 试卷管理Controller
 * 
 * @author ruoyi
 * @date 2021-01-25
 */
@Controller
@RequestMapping("/web/examPaper")
public class ExamPaperController extends BaseController
{
    private String prefix = "web/examPaper";

    @Autowired
    private IExamPaperService examPaperService;

    @Autowired
    private IQuesInfoService quesInfoService;

    @RequiresPermissions("web:examPaper:view")
    @GetMapping()
    public String examPaper()
    {
        return prefix + "/examPaper";
    }

    /**
     * 查询试卷管理列表
     */
    @RequiresPermissions("web:examPaper:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ExamPaper examPaper)
    {
        startPage();
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        return getDataTable(list);
    }


    /**
     * 导出试卷管理列表
     */
    @RequiresPermissions("web:examPaper:export")
    @Log(title = "试卷管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ExamPaper examPaper)
    {
        List<ExamPaper> list = examPaperService.selectExamPaperList(examPaper);
        ExcelUtil<ExamPaper> util = new ExcelUtil<ExamPaper>(ExamPaper.class);
        return util.exportExcel(list, "examPaper");
    }

    /**
     * 新增试卷管理
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }
    @GetMapping("/create")
    public String create()
    {
        return prefix + "/create";
    }

    /**
     * 新增保存试卷管理
     */
    @RequiresPermissions("web:examPaper:add")
    @Log(title = "试卷管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ExamPaper examPaper)
    {
        return toAjax(examPaperService.insertExamPaper(examPaper));
    }

    /**
     * 修改试卷管理
     */
    @GetMapping("/edit/{examId}")
    public String edit(@PathVariable("examId") Long examId, ModelMap mmap)
    {
        ExamPaper examPaper = examPaperService.selectExamPaperById(examId);
        mmap.put("examPaper", examPaper);
        return prefix + "/edit";
    }

    /**
     * 修改保存试卷管理
     */
    @RequiresPermissions("web:examPaper:edit")
    @Log(title = "试卷管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ExamPaper examPaper)
    {
        return toAjax(examPaperService.updateExamPaper(examPaper));
    }

    /**
     * 删除试卷管理
     */
    @RequiresPermissions("web:examPaper:remove")
    @Log(title = "试卷管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(examPaperService.deleteExamPaperByIds(ids));
    }

    @PostMapping( "/createDone")
    @ResponseBody
    public AjaxResult createDone(@RequestBody ExamPaper examPaper)
    {
        if (StringUtils.isEmpty(examPaper.getQuesIds())){
            return error("此试卷类型下无对应的题库");
        }
        return toAjax(examPaperService.insertExamPaper(examPaper));
    }

    @GetMapping("/toDetail/{quesIds}")
    public String toDetail(@PathVariable("quesIds") String quesIds,ModelMap mmap)
    {
       List<QuesInfo> list= quesInfoService.queryListByIds(quesIds);
        mmap.put("list", list);
        return prefix + "/detail";
    }

    @GetMapping("/selectUser/{examId}")
    public String toDetail(@PathVariable("examId") Long examId,ModelMap mmap)
    {
        mmap.put("examId",examId);
        return prefix + "/selectUser";
    }

}
